//
//  leverSheet.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/7/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class LeverSheet: SelectBaseSheet {
    var model: LeverCapitalModel?
    var config:LeverConfig?
    var leverView:LeverCollectionView?
    var limitLeverView:LeverCollectionView?
    var limitLine:UIView?
    var viewModel:LeverSheetViewModel?
    var leverTradeBtn:UIButton?
    
    init(model:SheetModel,coinfig: SelectConfig, dataSource: SelectmangerDelegate) {
        super.init(model: model, coinfig: coinfig, titleArr: [], selectStr: nil, dataSource: dataSource)
        viewModel = LeverSheetViewModel()
        self.model = model as? LeverCapitalModel
        self.config = coinfig as? LeverConfig
        self.setUpUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func setUpUI() {
        super.setUpUI()
        config?.viewHeight = (config?.LeverFootHeight)! + (config?.LeverHeadHeight)! + (config?.leverdefaloutHeight)! + (isiPhoneX() ? 34 : 0)
        let height = (kSCREEN_HEIGHT - (config?.viewHeight)!)
        selectView.frame = CGRect(x: 0, y:height, width: kSCREEN_WIDTH, height: (config?.viewHeight)!)
        let sheetView = LeverCollectionView(config: config!,model:model!, dataSource: self.dataSource!, style:.commonStyle)
        self.leverView = sheetView
        sheetView.themeBackground = ThemeManager.ThemeColorKeys.background
        sheetView.delegate = self
        selectView.addSubview(sheetView)
        sheetView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(65)
            make.height.equalTo((config?.leverdefaloutHeight)!)
        }
        
        limitLeverView = LeverCollectionView(config: config!,model:model!, dataSource: self.dataSource!, style:.limiteStyle)
        limitLeverView?.delegate = self
        limitLeverView?.isHidden = true
        limitLeverView?.themeBackground = ThemeManager.ThemeColorKeys.background
        selectView.addSubview(limitLeverView!)
        limitLeverView?.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(65)
            make.height.equalTo((config?.leverLimitedHeight)!)
        }
        
        
        let headview = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: (config?.LeverHeadHeight)!))
        selectView.addSubview(headview)
        headview.themeBackground = ThemeManager.ThemeColorKeys.background
        let nameArr = [kText("市价"),kText("限价")]
        for i in 0..<nameArr.count {
            let btn = UIButton()
            btn.tag = i + CoinBtnTag
            btn.addTarget(self, action: #selector(selectAction(sender:)), for: UIControlEvents.touchUpInside)
            btn.setTitle(nameArr[i], for: UIControlState.normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            headview.addSubview(btn)
            if i == 0 {
                btn.setTitleColor(kThemeColor, for: UIControlState.normal)
                limitLine = UIView()
                limitLine?.backgroundColor = kThemeColor
                headview.addSubview(limitLine!)
                limitLine?.snp.makeConstraints({ (make) in
                    make.centerX.equalTo(btn)
                    make.bottom.equalToSuperview().offset(-10)
                    make.height.equalTo(2)
                    make.width.equalTo(15)
                })
            }else {
                btn.themeSetTitleColor(ThemeManager.ThemeColorKeys.text1, for: UIControlState.normal)
            }
            btn.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(0)
                make.bottom.equalToSuperview()
                make.right.equalTo(headview.snp.centerX).offset(i == 0 ? 0 : 40)
                make.width.equalTo(40)
            }
        }
        
        let footView = UIView()
        footView.themeBackground = ThemeManager.ThemeColorKeys.background
        selectView.addSubview(footView)
        footView.snp.makeConstraints { (make) in
            make.height.equalTo(55 + (isiPhoneX() ? 34 : 0))
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(0)
        }
        
        let buyBtn = UIButton(frame: CGRect(x: 15, y: 0, width: kSCREEN_WIDTH-30, height: 40))
        leverTradeBtn = buyBtn
        buyBtn.addTarget(self, action: #selector(buyBtnAction), for: UIControlEvents.touchUpInside)
        buyBtn.backgroundColor = UIColor(hexStr: "#3cb265")
        buyBtn.layer.cornerRadius = 3
        buyBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
//        buyBtn.setTitle(String(format: "%@ %@0", kText("付款"),AppCacheInfo.default.currency == "CNY" ? "¥" : "$"), for: UIControlState.normal)
        buyBtn.setTitleColor(UIColor(hexStr: "#fdfdfd"), for: UIControlState.normal)
        footView.addSubview(buyBtn)
        
    }
    
    /// 杠杆买入 卖出基本信息
    ///
    /// - Parameter model: 数据源
    func leverRefresh(model:LeverTradeModel)  {
        self.viewModel?.tradeInfoModel = model
        self.viewModel?.coninKidArray = model.coninKidArray
        viewModel?.curretSelectModel = (viewModel?.markModel)!
        self.viewModel?.changeTradeInfo()
        self.viewModel?.markModel.isBuyBool = model.isBuyBool
        self.viewModel?.limitModel.isBuyBool = model.isBuyBool
        self.limitLeverView?.setModel(viewModel:viewModel!)
        self.leverView?.setModel(viewModel: viewModel!)
        
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
extension LeverSheet:leverDelegate {
    @objc func buyBtnAction() {
        if !(viewModel?.checkStates())! {
            return
        }
        
        if dataSource != nil && dataSource?.selectModel != nil {
            model?.model = (viewModel?.curretSelectModel)!
            model?.isMarketBool = (viewModel?.isMarketBool)!
            dataSource?.selectModel(model: model!)
        }
    }
    
    func updateTradePrice() {
        leverTradeBtn?.setTitle(String(format: "%@ %@", kText("付款"),(viewModel?.curretSelectModel.tradeName)!), for: UIControlState.normal)
        leverTradeBtn?.backgroundColor = (viewModel?.curretSelectModel.isBuyBool)! ? kGreenColor  : kRedColor
    }
    
    //Mark  leverDelegate {
    func upHeight(openBool:Bool) {
        selectView.layer.removeAllAnimations()
        let changeHeight = openBool ? config?.leverOpenHeight : config?.leverdefaloutHeight
        config?.viewHeight = (config?.LeverFootHeight)! + (config?.LeverHeadHeight)! + changeHeight! + (isiPhoneX() ? 34 : 0)
        let height = (kSCREEN_HEIGHT - (config?.viewHeight)!)
        selectView.frame = CGRect(x: 0, y:height, width: kSCREEN_WIDTH, height: (config?.viewHeight)!)
        leverView?.snp.updateConstraints { (make) in
            make.height.equalTo(changeHeight!)
        }
    }
    
    @objc func selectAction(sender:UIButton) {
        selectView.layer.removeAllAnimations()
        let markBtn:UIButton = selectView.viewWithTag(CoinBtnTag) as! UIButton
        let xjBtn:UIButton = selectView.viewWithTag(CoinBtnTag+1) as! UIButton
        leverView?.endEditing(true)
        limitLeverView?.endEditing(true)
        if sender.tag  == CoinBtnTag {
            viewModel?.isMarketBool = true
            leverView?.isHidden = false
            limitLeverView?.isHidden = true
            let changeHeight = leverView?.frame.size.height
            config?.viewHeight = (config?.LeverFootHeight)! + (config?.LeverHeadHeight)! + changeHeight! + (isiPhoneX() ? 34 : 0)
            let height = (kSCREEN_HEIGHT - (config?.viewHeight)!)
            selectView.frame = CGRect(x: 0, y:height, width: kSCREEN_WIDTH, height: (config?.viewHeight)!)
            markBtn.setTitleColor(kThemeColor, for: UIControlState.normal)
            xjBtn.themeSetTitleColor(ThemeManager.ThemeColorKeys.text1, for: UIControlState.normal)
            limitLine?.snp.remakeConstraints({ (make) in
                make.centerX.equalTo(markBtn)
                make.bottom.equalToSuperview().offset(-10)
                make.height.equalTo(2)
                make.width.equalTo(15)
            })
        }else {
            viewModel?.isMarketBool = false
            leverView?.isHidden = true
            limitLeverView?.isHidden = false
            let changeHeight = config?.leverLimitedHeight
            config?.viewHeight = (config?.LeverFootHeight)! + (config?.LeverHeadHeight)! + changeHeight! + (isiPhoneX() ? 34 : 0)
            let height = (kSCREEN_HEIGHT - (config?.viewHeight)!)
            selectView.frame = CGRect(x: 0, y:height, width: kSCREEN_WIDTH, height: (config?.viewHeight)!)
            xjBtn.setTitleColor(kThemeColor, for: UIControlState.normal)
            markBtn.themeSetTitleColor(ThemeManager.ThemeColorKeys.text1, for: UIControlState.normal)
            limitLine?.snp.remakeConstraints({ (make) in
                make.centerX.equalTo(xjBtn)
                make.bottom.equalToSuperview().offset(-10)
                make.height.equalTo(2)
                make.width.equalTo(15)
            })
        }
        self.updateTradePrice()
    }
}
